package monster.com.cvh.adapter;

import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.AppCompatSpinner;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import monster.com.cvh.R;
import monster.com.cvh.application.TinkerApplicationLike;
import monster.com.cvh.bean.ProfessionSkillBean;

/**
 * @author: 13580
 * @data: 2018/3/20
 * @function:荣誉证书adapter
 */

public class ProfessionSkillAdapter extends BaseQuickAdapter<ProfessionSkillBean.DataBean, BaseViewHolder> {
    private final String[] mList;

    public ProfessionSkillAdapter(@Nullable List<ProfessionSkillBean.DataBean> data) {
        super(R.layout.item_profession_skill, data);
        mList = TinkerApplicationLike.getContext().getResources().getStringArray(R.array.profession_sping_arr);
    }

    @Override
    protected void convert(final BaseViewHolder helper, final ProfessionSkillBean.DataBean item) {
        helper.setText(R.id.tv_item_profession_skill_title, mContext.getResources().getString(R.string.fragment_professional_skill_title) + (helper.getLayoutPosition() + 1));
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, R.layout.simple_spinner_item, mList) {
            @Override
            public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
                TextView tvTitle = (TextView) LayoutInflater.from(mContext).inflate(R.layout.item_profession_skill_selection, parent, false);
                tvTitle.setText(mList[position]);
                return tvTitle;
            }
        };
        AppCompatSpinner spinner = helper.getView(R.id.spinner_item_profession_skill);
        spinner.setAdapter(adapter);
        //监听选中
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                item.setLevel(mList[position]);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        spinner.setSelection(getSelectedIndex(item.getLevel()));

        EditText edtSkill = (EditText) helper.getView(R.id.edt_item_profession_skill_skill);
        //防止notifyDataChange()导致值Editext重绘触发TextWatcher造成数据错乱
        if (edtSkill.getTag() instanceof TextWatcher) {
            edtSkill.removeTextChangedListener((TextWatcher) edtSkill.getTag());
        }
        edtSkill.setText(item.getName());
        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                item.setName(s.toString().trim());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };
        edtSkill.addTextChangedListener(watcher);
        edtSkill.setTag(watcher);

        //如果只有一条item时，删除按钮不显示
        if (mData.size() == 1) {
            helper.setVisible(R.id.iv_item_profession_skill_delete, false);
        } else {
            helper.setVisible(R.id.iv_item_profession_skill_delete, true);
        }
        //当数据长度等3时，footer隐藏
        if (mData.size() == 8) {
            getFooterLayout().setVisibility(View.GONE);
        } else {
            getFooterLayout().setVisibility(View.VISIBLE);
        }
        //如果数据长度大于1，可以删除当前item
        helper.getView(R.id.iv_item_profession_skill_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mData.size() > 1) {
                    //初始化删除弹窗
                    AlertDialog alertDialog = new AlertDialog.Builder(mContext)
                            .setMessage("是否删除职业技能"+(helper.getLayoutPosition()+1)+"?")
                            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            })
                            .setPositiveButton(R.string.edt_info_delete, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    ProfessionSkillAdapter.this.remove(helper.getAdapterPosition());
                                    notifyDataSetChanged();
                                    dialog.dismiss();
                                }
                            })
                            .create();
                    alertDialog.show();
                }
            }
        });

    }

    private int getSelectedIndex(@NonNull String level) {
        for (int i = 0; i < mList.length; i++) {
            if (mList[i].equals(level)){
                return i;
            }
        }
        return 0;
    }
}
